package com.example.service.impl;

import com.example.mapper.ContentcategoryMapper;
import com.example.mapper.ItemcatMapper;
import com.example.pojo.ItemCat;
import com.example.pojo.ItemCatExample;
import com.example.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private ItemcatMapper itemcatMapper;

    @Override
    public List<ItemCat> queryByParentId(Long parentId) {
        ItemCatExample example = new ItemCatExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        return itemcatMapper.selectByExample(example);
    }

    @Override
    public List<ItemCat> queryAll() {

        return itemcatMapper.selectByExample(null);
    }

    @Override
    public List<Long> queryParentById(Long id) {
        List<Long> rs = new ArrayList<>();
        rs.add(id);

        do {
            ItemCat itemCat = itemcatMapper.selectByPrimaryKey(id);
            id = itemCat.getParentId();
            if (id != 0){
                rs.add(id);
            }
        }while (id != 0);
        Collections.reverse(rs);
        return rs;
    }
}
